package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.WXPAY;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.RechargeAdapter;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.RechargeBean;
import com.luoxiang.socialandroid.bean.UserInfoBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.NetUtils2;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class VipActivity extends BaseActivity {
    private ImageView icon_back, im_icon, im_vip, im_level;
    private TextView tx_name, tx_zunjing, tx_daoqi, tx_cz;
    private RecyclerView rc_recharge;
    private RechargeAdapter adapter;
    private ArrayList<String> list = new ArrayList<>();
    private TabLayout tab_vip;
    private String token;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";
    private int identity;
    private String vipflag;
    private ScrollView scrollView;
    private ConstraintLayout constraintTop;
    private ImageView icon_back1;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 0) {
                    constraintTop.setVisibility(View.GONE);
                } else {
                    constraintTop.setVisibility(View.VISIBLE);
                }
            }
        });
        icon_back1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_vip);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        showLoading();
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        icon_back = findViewById(R.id.icon_back);
        tab_vip = findViewById(R.id.tab_vip);
        im_icon = findViewById(R.id.im_icon);
        im_vip = findViewById(R.id.im_vip);
        tx_cz = findViewById(R.id.tx_cz);
        im_level = findViewById(R.id.im_level);
        tx_name = findViewById(R.id.tx_name);
        tx_zunjing = findViewById(R.id.tx_zunjing);
        tx_daoqi = findViewById(R.id.tx_daoqi);
        rc_recharge = findViewById(R.id.rc_recharge);
        scrollView = findViewById(R.id.scrollView);
        constraintTop = findViewById(R.id.constraintTop);
        icon_back1 = findViewById(R.id.icon_back1);
        list.add(getString(R.string.mi_huangjin_vip));
        list.add(getString(R.string.mi_bojin_vip));
        list.add(getString(R.string.mi_zuanshi_vip));
        for (String s : list) {
            tab_vip.addTab(tab_vip.newTab().setText(s));
        }
        tab_vip.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String tx = tab.getText().toString();
                if (adapter != null) {
                    adapter.setIndex(tab.getPosition());
                    adapter.notifyDataSetChanged();
                }
                if (tx.equals(getString(R.string.mi_huangjin_vip))) {
                    LiveEventBus.get("sendmsghj").post(null);
                    im_vip.setImageResource(R.mipmap.icon_tab_huangjin);
                    vipflag = getString(R.string.mi_huangjin_vip);
                } else if (tx.equals(getString(R.string.mi_bojin_vip))) {
                    im_vip.setImageResource(R.mipmap.icon_tab_bojin);
                    vipflag = getString(R.string.mi_bojin_vip);
                } else if (tx.equals(getString(R.string.mi_zuanshi_vip))) {
                    im_vip.setImageResource(R.mipmap.icon_tab_zuanshi);
                    vipflag = getString(R.string.mi_zuanshi_vip);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        getRequest();
        getVipPackages(token, EXTRA);
        getHomeUserInfo(token, EXTRA);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getVipPackages(token, EXTRA);
        getHomeUserInfo(token, EXTRA);
    }

    private void getHomeUserInfo(String token, String extra) {
        NetUtils2.getInstance().getApiService().getHomeUserInfo(token, extra)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr0", decryptStr);
                            Gson gson = new Gson();

                            UserInfoBean bean = gson.fromJson(decryptStr, UserInfoBean.class);
                            Glide.with(MyAppliaction.getContext())
                                    .load(url + bean.getData().getUser().getIcon())
                                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                                    .into(im_icon);
                            tx_name.setText(bean.getData().getUser().getName());
                        } catch (Exception e) {
                            LogUtils.i("decryptStr0", e.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getVipPackages(String token, String extra) {
        NetUtils2.getInstance().getApiService().getVipPackages(token, extra, 0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStrvip", decryptStr);
                            Gson gson = new Gson();
                            RechargeBean bean = gson.fromJson(decryptStr, RechargeBean.class);
                            identity = bean.getData().getVip().getIdentity();
                            LogUtils.i("identitys", identity);
                            com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(WXPAY, bean.getData().getVip().getWxPay());
                            switch (identity) {
                                case 0:
                                    tx_zunjing.setText(R.string.mi_ninmuqianhaishiputongyonghu);
                                    tx_daoqi.setText(R.string.mi_lijishengji);
                                    tx_cz.setText(R.string.mi_zanweikaitonghuiyuan);
                                    tab_vip.getTabAt(0).select();
                                    tx_daoqi.setOnClickListener(new CustomClickListener() {
                                        @Override
                                        protected void onSingleClick() {
                                            scrollView.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
                                        }

                                        @Override
                                        protected void onFastClick() {

                                        }
                                    });
                                    break;
                                case 10:
                                    im_level.setImageResource(R.mipmap.icon_huangjin);
                                    tx_zunjing.setText(R.string.mi_zunjingdehuangjinhuiyuan);
                                    tx_cz.setText(getString(R.string.mi_ninhaixuchongzhi) + (int) bean.getData().getVips().get(1).getAmount() + getString(R.string.mi_jiesuobojindengji));
                                    tab_vip.getTabAt(0).select();
                                    if (bean.getData().getVip().getEndTime() != null) {
                                        tx_daoqi.setText(bean.getData().getVip().getEndTime().substring(0, 10) + getString(R.string.mi_daoqi));

                                    }
                                    break;
                                case 20:
                                    im_level.setImageResource(R.mipmap.icon_bojin);
                                    tx_zunjing.setText(R.string.mi_zunjingdebojinhuiyuan);
                                    tx_cz.setText(getString(R.string.mi_ninhaixuchongzhi) + (int) bean.getData().getVips().get(2).getAmount() + getString(R.string.mi_jiesuozuanshidengji));
                                    tab_vip.getTabAt(1).select();
                                    if (bean.getData().getVip().getEndTime() != null) {
                                        tx_daoqi.setText(bean.getData().getVip().getEndTime().substring(0, 10) + getString(R.string.mi_daoqi));

                                    }
                                    break;
                                case 30:
                                    im_level.setImageResource(R.mipmap.icon_zuanshi);
                                    tx_zunjing.setText(R.string.zunjingdezuanshihuiyuan);
                                    tx_cz.setText(R.string.mi_zuigaohuiyuan);
                                    if (bean.getData().getVip().getEndTime() != null) {
                                        tx_daoqi.setText(R.string.mi_zhongshenyouxiao);
                                    }
                                    tab_vip.getTabAt(2).select();
                                    break;
                                default:
                                    tx_zunjing.setText(R.string.mi_ninmuqianhaishiputongyonghu);
                                    tx_daoqi.setText(R.string.mi_lijishengji);
                                    tx_cz.setText(R.string.mi_zanweikaitonghuiyuan);
                                    tab_vip.getTabAt(0).select();
                                    break;
                            }
                            GridLayoutManager gridLayoutManager = new GridLayoutManager(VipActivity.this, 3);
                            rc_recharge.setLayoutManager(gridLayoutManager);
                            if (bean != null) {
                                if (bean.getData() != null) {
                                    adapter = new RechargeAdapter(bean.getData().getVips());
                                    rc_recharge.setAdapter(adapter);
                                    adapter.setOnItemClickListener((adapter, view, position) -> {
                                        if (position != VipActivity.this.adapter.getIndex()) {
                                            return;
                                        }
                                        Intent intent = new Intent(VipActivity.this, ChargeActivity.class);
                                        intent.putExtra("id", bean.getData().getVips().get(position).getId());
                                        intent.putExtra("details", bean.getData().getVips().get(position).getDetails().get(4));
                                        intent.putExtra("icon", bean.getData().getVips().get(position).getIcon());
                                        intent.putExtra("name", bean.getData().getVips().get(position).getName());
                                        intent.putExtra("amount", bean.getData().getVips().get(position).getAmount());
                                        intent.putExtra("ValidMonth", bean.getData().getVips().get(position).getValidMonth());
                                        startActivity(intent);
                                    });

                                }
                            }
                        } catch (Exception e) {
                            LogUtils.e("errorvip", e.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                    }
                });
    }
}
